const os = require('os');
const crypto = require('crypto');
const moment = require("moment");
const _ = require("underscore");
const Utils = {
    getIPAddress() {
        try {
            let interfaces = os.networkInterfaces();
            let wlan = interfaces.WLAN[0];
            let address = wlan.address;
            return address;
        } catch (e) {
            return 'error addr'
        }
    },
    encrypt: function (password) {
        let hash = crypto.createHash('sha256');
        let passwordHash = hash.update(password);
        let encryptPwd = passwordHash.digest('hex');
        return encryptPwd;
    },
    /**
     * 开始结束时间的类型处理
     *
     * @param {string} startTime  - 开始时间。
     * @param {string} endTime  - 结束时间。
     * @param {string} type  - 类型。
     * @return {object} {startTime:处理后的开始时间,endTime:处理后的结束时间,queryGroupTime:处理后时间的sql}。
     */
    timeComponent:function (startTime,endTime,type){
        let start = "";
        let end = "" ;
        let queryGroupTime = "";
        switch (type){
            case "hour":
                start = moment(startTime).format("YYYY-MM-DD HH:mm:ss");
                end = moment(endTime).format("YYYY-MM-DD HH:mm:ss");
                queryGroupTime = ` DATE_FORMAT(dd.data_time,"%Y-%m-%d %H:%i:%s") AS groupTime,`;
               break;
            case "day":
                start = moment(startTime).format("YYYY-MM-DD 00:00:00");
                end = moment(endTime).format("YYYY-MM-DD 23:59:59");
                queryGroupTime = ` DATE_FORMAT(dd.data_time,"%Y-%m-%d") AS groupTime,`;
               break;
            case "month":
                start = `${moment(startTime).format("YYYY-MM")}-01 00:00:00`;
                end = moment(endTime).endOf("month").format("YYYY-MM-DD HH:mm:ss");
                queryGroupTime = ` DATE_FORMAT(dd.data_time,"%Y-%m") AS groupTime,`;
               break;
            case "year":
                start = null;
                end = null;
                queryGroupTime = ` DATE_FORMAT(dd.data_time,"%Y") AS groupTime,`;
               break;
        }
        return {startTime:start,endTime:end,queryGroupTime:queryGroupTime}
    },
    /**
     * 开始结束时间的类型处理,将数据数组根据类别字段名称筛选成chart所需格式。
     *
     * @param {Array} dataList  - 数据数组。
     * @param {object} columnNameObj  - 字段名称。
     * @return {object} chart数据信息。
     */
    chartDataProcessing:function (dataList,columnNameObj){
        let deviceObj = {};
        let chartObj = {
            time:[],
            data:[],
        }
        let chartDataList = _.sortBy(dataList,function (data){ return moment(data.dataCreateTime ? data.dataCreateTime : data.dataTime).valueOf()})
        for(const data of chartDataList){
            for(const dataName in columnNameObj){
                if(deviceObj[dataName]){
                    deviceObj[dataName].data.push(data[dataName] ? parseFloat(data[dataName]) : data[dataName]);
                }else{
                    deviceObj[dataName] = {
                        name:columnNameObj[dataName],
                        nameRow:dataName,
                        data:[data[dataName] ? parseFloat(data[dataName]) : data[dataName]]
                    }
                }
            }
            chartObj.time.push(data.dataTime);
        }
        for(const column in deviceObj){
            chartObj.data.push(deviceObj[column]);
        }
        return chartObj
    }

}
module.exports = Utils;